package org.jeecg.modules.basic.worker.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.basic.worker.entity.LwgyMaterialWorker;
import org.jeecg.modules.basic.worker.entity.LwgyMaterialWorkerUsed;
import org.jeecg.modules.basic.worker.service.ILwgyMaterialWorkerService;
import org.jeecg.modules.basic.worker.service.ILwgyMaterialWorkerUsedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

/**
* @Description: 物料管理个人管理
* @Author: jeecg-boot
* @Date:   2021-04-06
* @Version: V1.0
*/
@Api(tags="物料管理个人管理")
@RestController
@RequestMapping("/worker/lwgyMaterialWorkerUsed")
@Slf4j
public class LwgyMaterialWorkerUsedController extends JeecgController<LwgyMaterialWorkerUsed, ILwgyMaterialWorkerUsedService> {
   @Autowired
   private ILwgyMaterialWorkerUsedService lwgyMaterialWorkerUsedService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;


    /**
     * 分页列表查询
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "物料管理个人管理-分页列表查询")
    @ApiOperation(value="物料管理个人管理-分页列表查询", notes="物料管理个人管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(LwgyMaterialWorkerUsed lwgyMaterialWorkerUsed,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<LwgyMaterialWorkerUsed> queryWrapper = QueryGenerator.initQueryWrapper(lwgyMaterialWorkerUsed, req.getParameterMap());
        Page<LwgyMaterialWorkerUsed> page = new Page<LwgyMaterialWorkerUsed>(pageNo, pageSize);
        IPage<LwgyMaterialWorkerUsed> pageList = lwgyMaterialWorkerUsedService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "物料管理个人管理-分页列表查询")
    @ApiOperation(value="物料管理个人管理-分页列表查询", notes="物料管理个人管理-分页列表查询")
    @GetMapping(value = "/list1")
    public Result<?> list1(LwgyMaterialWorkerUsed lwgyMaterialWorkerUsed,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<LwgyMaterialWorkerUsed> queryWrapper = QueryGenerator.initQueryWrapper(lwgyMaterialWorkerUsed, req.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
        if (!roles.contains("admin")) {
            queryWrapper.eq("user_id",sysUser.getId());
        }
        Page<LwgyMaterialWorkerUsed> page = new Page<LwgyMaterialWorkerUsed>(pageNo, pageSize);
        IPage<LwgyMaterialWorkerUsed> pageList = lwgyMaterialWorkerUsedService.queryPageCustomer(page,queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "物料管理个人管理-分页列表查询")
    @ApiOperation(value="物料管理个人管理-分页列表查询", notes="物料管理个人管理-分页列表查询")
    @GetMapping(value = "/list2")
    public Result<?> list2(LwgyMaterialWorkerUsed lwgyMaterialWorkerUsed,
                           @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                           @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                           @RequestParam(name="id") String id,
                           HttpServletRequest req) {
        QueryWrapper<LwgyMaterialWorkerUsed> queryWrapper = QueryGenerator.initQueryWrapper(lwgyMaterialWorkerUsed, req.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
        if (!roles.contains("admin")) {
            queryWrapper.eq("user_id",sysUser.getId());
        }
        Page<LwgyMaterialWorkerUsed> page = new Page<LwgyMaterialWorkerUsed>(pageNo, pageSize);
        IPage<LwgyMaterialWorkerUsed> pageList = lwgyMaterialWorkerUsedService.queryPageHistoryCustomer(page,queryWrapper,id);
        return Result.OK(pageList);
    }


    /**
     *   添加
     *
     * @param
     * @return
     */
    @AutoLog(value = "物料管理个人管理-添加")
    @ApiOperation(value="物料管理个人管理-添加", notes="物料管理个人管理-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody LwgyMaterialWorkerUsed lwgyMaterialWorkerUsed) {
        lwgyMaterialWorkerUsed.setOutNum(lwgyMaterialWorkerUsed.getOutNum());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        lwgyMaterialWorkerUsed.setOutDate(new Date());
        lwgyMaterialWorkerUsed.setUserId(sysUser.getUsername());
        Calendar cl = new GregorianCalendar();// 新建日期对象
        lwgyMaterialWorkerUsed.setYear(String.valueOf(cl.get(Calendar.YEAR)) + "年");
        lwgyMaterialWorkerUsed.setMonth(String.valueOf(cl.get(Calendar.MONTH) + 1) + "月");
        lwgyMaterialWorkerUsed.setDay(String.valueOf(cl.get(Calendar.DAY_OF_MONTH)) + "日");
        lwgyMaterialWorkerUsedService.save(lwgyMaterialWorkerUsed);
        LwgyMaterialWorker wgoods = new LwgyMaterialWorker();
        BigDecimal a=wgoods.getApplyNum();
        BigDecimal b=lwgyMaterialWorkerUsed.getOutNum();
        BigDecimal c = a.subtract(b);
        wgoods.setApplyNum(c);
        BigDecimal zx = wgoods.getOutNum();
        BigDecimal e=lwgyMaterialWorkerUsed.getOutNum();
        BigDecimal f = zx.add(e);
        wgoods.setOutNum(f);
        wgoods.setApplyDate(new Date());
        wgoods.setApplyId(sysUser.getUsername());
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param
     * @return
     */
    @AutoLog(value = "物料管理个人管理-编辑")
    @ApiOperation(value="物料管理个人管理-编辑", notes="物料管理个人管理-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody  LwgyMaterialWorkerUsed lwgyMaterialWorkerUsed) {
        lwgyMaterialWorkerUsedService.updateById(lwgyMaterialWorkerUsed);
        return Result.OK("编辑成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "物料管理个人管理-通过id删除")
    @ApiOperation(value="物料管理个人管理-通过id删除", notes="物料管理个人管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        lwgyMaterialWorkerUsedService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "物料管理个人管理-批量删除")
    @ApiOperation(value="物料管理个人管理-批量删除", notes="物料管理个人管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.lwgyMaterialWorkerUsedService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "物料管理个人管理-通过id查询")
    @ApiOperation(value="物料管理个人管理-通过id查询", notes="物料管理个人管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       LwgyMaterialWorkerUsed lwgyMaterialWorkerUsed = lwgyMaterialWorkerUsedService.getById(id);
        if(lwgyMaterialWorkerUsed==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(lwgyMaterialWorkerUsed);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request,  LwgyMaterialWorkerUsed lwgyMaterialWorkerUsed) {
        return super.exportXls(request, lwgyMaterialWorkerUsed, LwgyMaterialWorkerUsed.class, "物料物品管理");
    }



    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response,LwgyMaterialWorkerUsed.class);
    }









}
